<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               :page.sync="page"
               v-model="form"
               ref="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange">
      <template slot="menuLeft">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-download"
                   plain
                   @click="bomDownload">下载
        </el-button>
      </template>
      <template slot="search" slot-scope="{row,size}">
        <el-form :inline="true">
          <el-form-item label="图纸编号:" style="margin-left: 20px;">
            <el-select v-model="search.drawingNumber" multiple placeholder="请选择图纸编号" :size="size" filterable >
              <el-option  v-for="item in drawingNumberList" :key="item.id" :label="item.drawingNumber" :value="item.drawingNumber">
                <span style="float: left">{{ item.drawingNumber }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>

      </template>
    </avue-crud>

    <div v-if="downloadOption">
      <el-dialog title="下载选项"
                 :visible.sync="downloadOption"
                 append-to-body
                 width="50%"
                 v-drag-dialog center
                 :close-on-click-modal='false'
                 @submit.native.prevent>
        <avue-form ref="downloadFormRef" v-model="addForm" :option="addOption"></avue-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="downloadOption = false" size="small" icon="el-icon-close">取 消</el-button>
          <el-button type="primary" @click="downloadMaterielBom('downloadFormRef')" size="small" icon="el-icon-check">确 定</el-button>
        </div>
      </el-dialog>
    </div>

  </basic-container>
</template>

<script>

  import {findDrawingWorkOrderList, getdrawingNumberList} from "@/api/drawing/drawingworkorder";
  import {getToken} from '@/util/auth';

  export default {
    data() {
      return {
        form: {},
        query: {},
        search:{
          drawingNumber:'',
        },
        drawingNumberList:[],
        loading: true,
        page: {
          pageSize: 100,
          currentPage: 1,
          total: 0
        },
        option: {
          menu: false,
          addBtn: false,
          editBtn:false,
          height: 580,
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          align:'center',
          menuAlign:'center',
          border: true,
          index: true,
          indexLabel:'序号',
          viewBtn: false,
          selection: false,
          dialogClickModal: false,
          column: [
            {
              label: "项目工单",
              prop: "workOrderNo",
              search: true,
              type: "tree",
              dicUrl: "/api/data-project/projectmana/getOrderAllList",
              props: {
                label: "workOrderNo",
                value: "workOrderNo"
              },
            },
            {
              label: "图纸编号",
              prop: "drawingNumber",
            },
            {
              label: "图纸名称",
              prop: "drawingName",
            },
            {
              label: "客户名称",
              prop: "customerName",
            },
            {
              label: "项目名称",
              prop: "projectName",
            },
            {
              label: "规格型号",
              prop: "drawingProperty",
            },
            {
              label: "数量",
              prop: "num",
            },
            {
              label: "单位",
              prop: "company",
            },
            {
              label: "材料/品牌",
              prop: "material",
            },
            {
              label: "备注",
              prop: "comment",
            },
            {
              label: "单据类型",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=bill_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "billType",
              search: true,
              hide: true
            },
          ]
        },
        data: [],
        downloadOption: false,
        addForm: {},
        addOption: {
          submitBtn:false,
          emptyBtn:false,
          column: [
            {
              label: "项目工单",
              prop: "workOrderNo",
              type: "tree",
              dicUrl: "/api/data-project/projectmana/getOrderAllList",
              props: {
                label: "workOrderNo",
                value: "workOrderNo"
              },
            },
            {
              label: "设备代号",
              prop: "drawingNumberList",
              type: 'tree',
              dicUrl: "/api/data-drawing-info/drawinginfo/getMHInfoList",
              props: {
                label: "drawingNumber",
                value: "drawingNumber"
              },
              multiple:true,
            },
            {
              label: "单据类型",
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=bill_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              prop: "billType",
              search: true,
              hide: true,
              rules: [{
                required: true,
                message: "请输入数据类型",
                trigger: "blur"
              }],
            },
          ]
        }
      };
    },
    created() {
       this.getdrawingNumberListFun();
    },
    methods: {
      searchReset() {
        this.query = {};
        //this.onLoad(this.page);
      },
      getdrawingNumberListFun(){
        getdrawingNumberList().then(res => {
          this.drawingNumberList = res.data.data;
        })
      },
      searchChange(params, done) {
        this.query = params;
        let drawingNumberList = {"drawingNumberList":this.search.drawingNumber};
        let serarchObj = Object.assign(params, drawingNumberList)
        console.log("obj----->"+JSON.stringify(serarchObj));
        this.page.currentPage = 1;
        this.onLoad(this.page, serarchObj);
        done();
      },
      selectionChange(list) {
        //this.selectionList = list;
      },
      selectionClear() {
        //this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
        this.onLoad(this.page, this.query);
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
        this.onLoad(this.page, this.query);
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        console.log("serach----->"+JSON.stringify(Object.assign(params, this.query)));
        findDrawingWorkOrderList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          let data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      bomDownload() {
        this.downloadOption = true;
        this.addForm.workOrderNo = '';
        this.addForm.drawingNumber = '';
        this.addForm.billType = '';
      },
      downloadMaterielBom(formName) {
        // 表单中的form
        let downloadOption =  this.$refs.downloadFormRef.form;

        if (downloadOption.billType == '') {
          this.$refs[formName].validate();
          return;
        }
        if (downloadOption.workOrderNo == '' && downloadOption.drawingNumberList == '') {
          this.$message.warning("请输入工单号或图纸代号!");
          return;
        }
        /*if (projectValue.projectCode === '' || projectValue.projectName === '' || projectValue.workOrder === '') {
          this.$refs[formName].validate();
          return;
        }*/
        let exportUrl = `/api/drawing/workorder/exportSetlCont?workOrderNo=`+ this.addForm.workOrderNo +`&drawingNumber=`+this.addForm.drawingNumberList+`&billType=`+this.addForm.billType+`&${this.website.tokenHeader}=${getToken()}`;
        console.log("url---->"+exportUrl);
        window.open(exportUrl)
        this.downloadOption = false;
      }
    }
  };
</script>

<style>

</style>
